#!/bin/bash
#
# Unified Plone installer build script
# Author: Ricardo Brito (ricardo at rbncurriculo.cjb.net)
#
#
#
PZP_HOME=PZP
PLONE_HOME=`pwd`/$PZP_HOME
PACKAGES_DIR=packages
TRUE=1
FALSE=0
ISROOT=$FALSE

#############################
# check running instal as root
if [ `whoami` != root ]; then
	ISROOT=$TRUE
fi
 
echo -n "Set directory to install or only press enter for default (default is '$PLONE_HOME'):"
read VAR

CHECK=$(find $VAR -maxdepth 0 -type d \( -user `whoami` -perm /u+rwx -o -group `groups` -perm /g+rwx -o -perm /o+w \) 2> /dev/null |wc -l )

if [ $CHECK = 0 ]; then
	echo "directory not found or there aren't permission to write at '$VAR'"
	exit 1
fi

if [ ! -z $VAR ]; then
	PLONE_HOME=$VAR/$PZP_HOME
	echo "The new directory is '$PLONE_HOME'"
fi

# Capture current working directory for build script
PWD=`pwd`
CWD="$PWD"

PACKAGES_DIR=packages
PKG=$CWD/$PACKAGES_DIR

# echo $PLONE_HOME

####################  FUNCTIONS  ##################################

function _YESNO(){
	FALSE=0
	TRUE=1

	retorno=$FALSE
	if [ $1 = 'yes' -o $1 = 'y' -o $1 = 'Yes' -o $1 = 'YES' -o $1 = 'Y' -o $1 = '1' ]; then
		retorno=$TRUE
	else 
		if [ $1 = 'no' -o $1 = 'n' -o $1 = 'No' -o $1 = 'NO' -o $1 = 'N' -o $1 = '0' ]; then
			retorno=$FALSE
		fi
	fi
	echo $retorno "//function _YESNO()"
	return $retorno
}


##############################################################
# Exit if potential conflict with other install at $PLONE_HOME
if [ -e $PLONE_HOME ]; then
    echo ""
    echo "An existing install was detected at '$PLONE_HOME', select other place."
    echo "Install aborted"
    exit 1
fi

# Abort install if no gcc
if [ ! -e /usr/bin/gcc ]; then
        echo "Note: gcc is required for the install. Exiting now."
        exit 1
fi

## Abort install if no g++
#if [ ! -e /usr/bin/g++ ]; then
#    echo "Note: g++ is required for the install. Exiting now."
#    exit 1
#fi

# Abort install if no make
if [ ! -e /usr/bin/make ]; then
    echo "Note: make is required for the install. Exiting now."
    exit 1
fi  
    
# Abort install if this script is not run from within it's parent folder
if [ ! -e $PACKAGES_DIR ]; then
        echo "Note: This install script must be run within it's working directory ($PACKAGES_DIR)."
        exit 1
fi

echo $ISROOT "///check"

if [ $ISROOT -eq $TRUE ]; then
	ISLIB=$TRUE
	VAR=$TRUE
	echo "Do you install the libraries 'zlib'e 'jpegsrc'. If alredy install select no? [Y,n]"
	read VAR

#	_YESNO $VAR
#	VAR=$?

	_YESNO $VAR
	if [ $? -ne $ISLIB ]; then
		ISLIB=$FALSE
	fi
#	echo "$VAR - $ISLIB"
fi

if [ $ISLIB -eq $TRUE ]; then
	##################
	# build zlib 1.2.3
	echo "Compiling and installing zlib ..."
	cd $PKG
	tar -zxf $ZLIB_TB
	chmod -R 775 $ZLIB_DIR
	cd $ZLIB_DIR
	./configure -s; make test; make install
	cd $PKG
	if [ -d $ZLIB_DIR ]; then
	    rm -rf $ZLIB_DIR
	fi

	###################
	# build libjpeg v6b
	echo "Compiling and installing jpeg libraries ..."
	cd $PKG
	tar -zxf $JPEG_TB
	chmod -R 775 $JPEG_DIR
	cd $JPEG_DIR
	if [ ! -e /usr/local/man/man1 ]; then
	    mkdir /usr/local/man/man1
	fi
	./configure CFLAGS='-fPIC'; make; make install
	ranlib libjpeg.a
	cp libjpeg.a /usr/local/lib
	cp *.h /usr/local/include
	cd $PKG
	if [ -d $JPEG_DIR ]; then
	        rm -rf $JPEG_DIR
	fi

fi

######################################
# Build Python (with readline support)
# note: Install readline before running this script
echo "Installing Python 2.4.4..."
cd $PKG
tar -zxf $PYTHON_TB
chmod -R 775 $PYTHON_DIR
cd $PYTHON_DIR
./configure --prefix=$PY_HOME --with-readline --with-zlib
make
make install
cd $PKG
if [ -d $PYTHON_DIR ]
then
        rm -rf $PYTHON_DIR
fi

#########################
# install ReportLab 2.0
echo "Installing ReportLab (PDF toolkit)..."
cd $PKG
tar -zxf $REPLAB_TB
mv $REPLAB_DIR/reportlab $SITE_PACKAGES/reportlab
cd $PKG
if [ -d $REPLAB_DIR ]
then
        rm -rf $REPLAB_DIR
fi

###################
# build PyXML 0.8.4
echo "Compiling and installing PyXML ..."
cd $PKG
tar -zxf $PYXML_TB
chmod -R 775 $PYXML_DIR
cd $PYXML_DIR
$PY ./setup.py build
$PY ./setup.py install
cd $PKG
if [ -d $PYXML_DIR ]; then
        rm -rf $PYXML_DIR
fi

#####################
# install ElementTree
echo "Installing ElementTree ..."
cd $PKG
tar -zxf $ETREE_TB
chmod -R 775 $ETREE_DIR
cd $ETREE_DIR
$PY ./setup.py build
$PY ./setup.py install
cd $PKG
if [ -d $ETREE_DIR ]; then
        rm -rf $ETREE_DIR
fi

##################
# build Zope 2.9.6
echo "Compiling and installing Zope 2.9.6 ..."
cd $PKG
tar -zxf $ZOPE_TB
chmod -R 775 $ZOPE_DIR
cd $ZOPE_DIR
./configure --with-python=$PY --prefix=$PLONE_HOME
make; make install
cd $PKG
if [ -d $ZOPE_DIR ]; then
        rm -rf $ZOPE_DIR
fi

##########################
# Generate random password
echo "Generating random password ..."
cd $PKG
tar -zxf $HSCRIPTS_TB
chmod -R 775 $HSCRIPTS_DIR
cd $HSCRIPTS_DIR
PASSWORD_SCRIPT=./generateRandomPassword.py
PASSWORD=`$PY $PASSWORD_SCRIPT`
cd $PKG

exit 1
